package donnees.dao;

import donnees.modele.Film;
import donnees.outils.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class FilmDao implements FilmInterface {

    private Session session;

    @Override
    public List listerFilm() throws Exception {
        session = HibernateUtil.getSessionFactory().openSession();
        List results;
        Criteria cr = session.createCriteria(Film.class);
        results = (List) cr.list();
        session.close();
        if (results.isEmpty()) {
            throw new Exception("Impossible de lister les films !");
        } else {
            return results;
        }
    }

    @Override
    public Film trouverFilm(String titre) throws Exception {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria cr = session.createCriteria(Film.class).add(Restrictions.eq("titre", titre));
        Film f = (Film) (cr.uniqueResult());
        session.close();
        if (f == null) {
            throw new Exception("Aucun film ne porte ce nom.");
        } else {
            return f;
        }
    }
}
